<template>
	<view class="content">
		<block v-if="!islogin">
			<view style="width: 100%;bottom: 10%; position: fixed;">
				<button type="primary" @click="scan">扫码登录</button>
			</view>
		</block>
		<block v-else>
			<web-view :src="weburl" use-cache></web-view>
		</block>
	</view>
</template>

<script>
	function getCurrentDate() {
		const date = new Date();
		const year = date.getFullYear();
		const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份 0-11，需 +1
		const day = String(date.getDate()).padStart(2, '0');
		return `${year}-${month}-${day}`;
	}

	export default {
		data() {
			return {
				refreshInterval: null,
				weburl: '',
				islogin: false
			}
		},
		// 在页面onLoad或onReady中
		onReady() {
			// 锁定横屏 
			plus.screen.lockOrientation('landscape');
		},
		// onLaunch() {
		// 	// 禁用页面恢复
		// 	if (plus && plus.webview) {
		// 		plus.webview.setBlockNetworkImage(false);
		// 		plus.webview.setHardwareAccelerated(true);
		// 	}
		// },
		// onShow() { 
		// 	var that = this;
		// 	const val = uni.getStorageSync('key');
		// 	if (that.weburl == 'http://qc.chinaliutan.com:4011/index.html#/login?sid=9A4C81B9' && val != '') {
		// 		that.login(that, val);
		// 	}
		// },
		onShow() {
			var that = this;
			var date2 = getCurrentDate();
			const date = uni.getStorageSync('date');
			const val = uni.getStorageSync('key');
			if (date2 == date && val != '') {
				that.login(that, val);
			}
		},
		onLoad() {
			// var that = this;
			// that.refreshInterval = setInterval(() => {
			// 	that.refreshData(that);
			// }, 10 * 60 * 1000); // 1.5小时转换为毫秒
			// }, 1.5 * 60 * 60 * 1000); // 1.5小时转换为毫秒

			// try {
			// 	const res = uni.getSystemInfoSync();
			// 	console.log(res.deviceId); // 唯一 

			// 	// 获取当前时间戳（毫秒级）
			// 	var timestamp = Date.now();
			// 	// 将时间戳转换为日期对象
			// 	var dateObject = new Date(timestamp);
			// 	// 将日期对象转换为时间戳
			// 	var newTimestamp = dateObject.getTime() + 3333333; 
			// 	that.weburl = "http://qc.chinaliutan.com:4011/go.html?username=24168&pwd=Abc)(24168&id=289&d=" +
			// 		newTimestamp; 
			// 	that.islogin = true; 
			// } catch (e) {
			// 	// 处理异常情况
			// 	console.error('获取系统信息失败', e);
			// } 
			// var date2 = getCurrentDate();
			// const date = uni.getStorageSync('date');
			// const val = uni.getStorageSync('key');
			// if (date2 == date && val != '') {
			// 	that.login(that, val);
			// }
		},
		methods: { 
			refreshData(that) {
				const val = uni.getStorageSync('key');
				if (val != '') {
					that.login(that, val);
				}
			},
			scan() {
				var that = this;
				// 调起条码扫描
				uni.scanCode({
					scanType: ['qrCode'],
					success: function(res) {
						// console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						// 获取当前时间戳（毫秒级）
						if (res.result != '') {
							that.login(that, res.result);
						}
					}
				});
			},
			login(that, val) {
				var timestamp = Date.now();
				// 将时间戳转换为日期对象
				var dateObject = new Date(timestamp);
				// 将日期对象转换为时间戳
				var newTimestamp = dateObject.getTime() + 750000;
				that.weburl = 'http://qc.chinaliutan.com:4011/go.html?' + val + '&d=' +
					newTimestamp;
				that.islogin = true;
				uni.setStorageSync('key', val);
				var date = getCurrentDate();
				uni.setStorageSync('date', date);
			}
		}
	}
</script>

<style>
	.content {
		width: 100%;
		height: 100%;
		background-image: url('/static/1.jpg');
	}
</style>